﻿#ifndef WEBSOCKETCLIENT_H
#define WEBSOCKETCLIENT_H

#include <QObject>
#include <QWebSocket>
#include <QProcess>
#include <QCoreApplication>
#include <QTimer>
#include <QElapsedTimer>

class WSClient : public QObject
{
    Q_OBJECT
public:
    explicit WSClient(QObject *parent = nullptr);
    ~WSClient();
    void wss_config();
    QAbstractSocket::SocketState connctionState();
    void creat_url();  //cmd调用python生成websocket连接的url
 
signals:
    void connection_state(const QString& msg, const bool state);    //连接状态发生变化时的通知信息
    void occur_error(const QString& errorString);
    void NewTextMessage(const QString& message);
    void NewBinaryMessage(const QByteArray& message);
 
public slots:
    void ConnectTo(const QString& url);
    void Disconnect();
    void sendTextMessage(const QString& message);
    void sendBinaryMessage(const QByteArray& data);
 
protected slots:
    void on_connected();
    void on_disconnected();
    void on_error(QAbstractSocket::SocketError error);
    void on_textMessageReceived(const QString& message);
    void on_binaryMessageReceived(const QByteArray& message);
 
private:
    QWebSocket* WSClientObj = nullptr;
    QProcess *pyProcess = nullptr;
    QString url = "";
    QTimer* timer = nullptr;
    QElapsedTimer ElapsedTimer;
};
 
#endif
